/**
 * Created by limx on 2016/4/26.
 */
$(function(){
    initSideBar(2);
})

/**
 * 保存供车信息
 */
function save_info() {
    if (checkInfo()) {
        //保存信息
        var id = $("#hidId").val();
        var cate_name = $("#cate_name").val();
        var index = $("#cate-info").children().length;
        var cate_info = new Array();
        for(var i=0;i<index;i++){
            var key = $("#k"+i).val();
            var name = $("#n"+i).val();
            var type = $("#t"+i).val();

            var item = new Array();

            item=[key,name,type];

            cate_info.push(item);

        }

        conLog(cate_info);

        var json = {
            "id": id,
            "cate_name": cate_name,
            "cate_info":cate_info
        };

        $.post("/Home/Cate/pfnAdd", json, function (jsonData) {
            if (jsonData.STATUS == "1") {
                location = "/Home/Cate/index";
            }
            else {
                showAlert(jsonData.MESSAGE);
            }
        }, "json")
    }
}

function checkInfo() {

    var cate_name = $("#cate_name").val();
    if(cate_name.trim()==""){
        showAlert("分类名称不能为空！");
        return false;
    }

    var index = $("#cate-info").children().length;
    if(index==0){
        showAlert("请增加分类字段！");
        return false;
    }
    for(var i=0;i<index;i++){
        var key = $("#k"+i).val();
        var name = $("#n"+i).val();
        var type = $("#t"+i).val();

        if (key.trim() == "") {
            showAlert("KEY不能为空！");
            return false;
        }

        if (name.trim() == "") {
            showAlert("字段名不能为空！");
            return false;
        }

        if (type.trim() == "") {
            showAlert("字段类型不能为空！");
            return false;
        }
    }

    return true;
}

/**
 * 新增分类字段
 */
function add_cate_info(){
    var index = $("#cate-info").children().length;
    var data='<div class="control-group cate-info">\
            <label class="control-label">分类字段</label>\
            <div class="controls">\
                <input id="k'+index+'" type="text" placeholder="key值" class="span11">\
            </div>\
            <div class="controls">\
                <input id="n'+index+'" type="text" placeholder="字段名" class="span11">\
            </div>\
            <div class="controls">\
            <select id="t'+index+'" class="span11">\
                <option value="0">字段类型：文本</option>\
                <option value="1">字段类型：时间</option>\
            </select>\
            </div>\
        </div>';

    $("#cate-info").append(data);

}